<template>
    <div class="medicine">
        <el-card class="main">
            <div class="topBtn">
                <div class="left">
                    <!-- 导出清单 -->
                    <el-button type="info" size="small" @click="resetList">
                        <i class="el-icon-refresh-left"></i>
                        重置</el-button>
                    <download-excel style="display:inline-block;margin-left:10px;"
                        :data="json_data" :fields="json_fields" :header="headers"
                        :name="name">
                        <el-button type="success" size="small">
                            <i class="el-icon-upload"></i>
                            导出</el-button>
                    </download-excel>
                </div>
                <div class="right">
                    <el-input placeholder="请输入内容" class="input-with-select"
                        v-model="searchInput">
                        <el-button slot="append" icon="el-icon-search"
                            @click="searchStockList"></el-button>
                    </el-input>
                </div>

            </div>
            <!-- 药品列表 -->
            <div class="medicineList">
                <el-table ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" :row-class-name="tableRowClassName"
                    style="width: 100%">
                    <el-table-column prop="type" label="类型" width="80">
                        <template slot-scope="scope">
                            <el-tag disable-transitions type="warning"
                                v-if="scope.row.type == 0">出库</el-tag>
                            <el-tag disable-transitions type="success"
                                v-if="scope.row.type == 1">入库</el-tag>

                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="药品名称" width="80">
                    </el-table-column>
                    <el-table-column prop="id" label="药品编号" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column prop="serialNumber" label="型号"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column prop="total" label="库存" width="80">
                    </el-table-column>
                    <el-table-column prop="num" label="数量" width="80">
                    </el-table-column>
                    <el-table-column prop="remarks" label="备注" width="120">
                    </el-table-column>
                    <el-table-column prop="userName" label="负责人"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column prop="status" label="状态" width="80">
                        <template slot-scope="scope">
                            <el-tag disable-transitions type="success"
                                v-if="scope.row.status == 0">有效</el-tag>
                            <el-tag disable-transitions type="info"
                                v-if="scope.row.status == 1">无效</el-tag>

                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center"
                        width="180">
                        <template slot-scope="scope">
                            <el-button type="primary" size="mini"
                                @click="changeRemark(scope.row.id)">
                                修改
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页器 -->
                <Pagination :total="total" :pageSize="pageSize"
                    @getPagination="getPagination"></Pagination>

                <!-- 模态框——修改备注 -->
                <el-dialog title="修改备注" :visible.sync="tipsDialogTableVisible">
                    <el-card>
                        <div style="margin: 20px 0;"></div>
                        <el-input type="textarea" placeholder="请输入备注..."
                            :autosize="{ minRows: 4, maxRows: 6 }"
                            v-model="changeTipsInput" maxlength="100"
                            show-word-limit>
                        </el-input>
                        <div style="margin: 20px 0;text-align: center;">
                            <el-button type="primary"
                                @click="changeTips">保存</el-button>
                        </div>
                    </el-card>
                </el-dialog>
            </div>
        </el-card>

    </div>
</template>

<script>
import Pagination from '@/components/Pagination'
export default {
    name: '',
    data() {
        return {
            //分页器
            total: 0,
            currentPage: 1,
            pageSize: 10,
            tableData: [],
            //模态框展开隐藏
            tipsDialogTableVisible: false,
            //模态框输入内容
            changeTipsInput: '',
            changeId: 0,
            //搜索
            searchInput: '',
            //导出清单需要数据
            headers: '药品库存列表',
            name: '药品库存清单',
            //配置表格标题字段对应文字
            json_fields: {
                // "订单编号":"code",
                //数字处理--
                "出库入库": "type",
                "药品名称": "name",
                "药品编号":"serialNumber",
                "库存":"total",
                "数量":"num",
                "备注":"remarks",
                "负责人":"userName",
                "创建时间":"createTime",
                "状态":"status",
            },
            json_data: []
        }
    },
    components: {
        Pagination
    },
    created() {
        this.getMedStockList(this.currentPage, this.pageSize)
    },
    methods: {
        //重置页面
        resetList(){
            this.getMedStockList(this.currentPage, this.pageSize)
        },
        //列表颜色区分
        tableRowClassName({ row, rowIndex }) {
            if (row.type == 0) {
                return 'warning-row';
            } else if (row.type == 1) {
                return 'success-row';
            }
        },
        //-------------------------------获取点击的页码
        getPagination(page) {
            console.log('----', page);
            this.currentPage = page;
            this.getMedStockList(this.currentPage, this.pageSize);
        },
        //-------------------------------获取药品列表
        async getMedStockList(currentPage, pageSize) {
            let res = await this.$api.medicine.getDrugStock({ currentPage, pageSize })
            //列表
            console.log(res.data.data.list);
            if (res.data.code === 200) {
                this.tableData = res.data.data.list;
                this.json_data=this.tableData;
                //页码
                this.total = res.data.data.total;
            } else {
                console.log('请求失败');
                this.tableData = []
                this.pageSize = 0;
                this.total = 1;
            }
        },
        async changeTips() {
            let res = await this.$api.medicine.changeDrugRemark(this.changeId, this.changeTipsInput)
            this.getMedStockList(this.currentPage, this.pageSize);
            this.tipsDialogTableVisible = false;
            this.changeTipsInput='';
        },
        changeRemark(id) {
            this.changeId = id;
            console.log(id);
            this.tipsDialogTableVisible = true
        },
        //搜索
        async searchStockList() {
            let con = this.searchInput
            let res = await this.$api.medicine.searchDrugStock(con, this.currentPage, this.pageSize);
            console.log(res);
            this.tableData=res.data.data.list;
            this.total=res.data.data.total;
        }
    }
}
</script>

<style lang="less" scoped>
.medicine {
    .main {
        background-color: rgba(128, 128, 128, 0.1);

        .topBtn {
            border-top: 3px solid #ccc;
            border-radius: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            padding: 5px 10px;
            background-color: #fff;
        }
    }
}

.el-table {
    /deep/.warning-row {
        background: rgba(255, 193, 193, 0.8);
    }
}

.el-table {
    /deep/ .success-row {
        background: rgba(204, 255, 204, 0.8);
    }
}
</style>